/*
 * @Author: error: git config user.name & please set dead value or install git
 * @Date: 2023-05-09 15:05:32
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2023-09-12 18:01:31
 * @FilePath: \stm32g431_AllRings\applications\main.c
 * @Description:
 *
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
 */
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-06     SummerGift   first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
// #include "spi_flash_sfud.h"
// #include "drv_spi.h"
#include "fdcan_app.h"
#include "my_fdcan_drv.h"

/* defined the LED2 pin: PA5 */
#define LED1_PIN GET_PIN(B, 0)

/********************************************************************/
/*
线程有异常情况时，请务必先检查一下系统的内存是否不够
首先检查是否分配了大容量的数组、静态内存池等
通过FINSH命令-free 查看系统内存使用情况，根据实际来调整业务代码中的内存分配等
*/
/********************************************************************/
#ifdef USE_SPI_FLASH
// 挂载spi-device 到 spi2 -bus 上
int spi2_device_operation(void)
{
    rt_pin_mode(NORFLASH_WP_PIN, PIN_MODE_OUTPUT); // 根据原理图，设置WP引脚为高

    rt_pin_write(NORFLASH_WP_PIN, PIN_HIGH);

    rt_hw_spi_device_attach("spi2", "spi20", GPIOB, GPIO_PIN_12); // 挂载spi20设备到SPI2总线上

    // HAL_Delay
    rt_sfud_flash_probe("ZD25WD40B", "spi20");

    return 0;
}
INIT_DEVICE_EXPORT(spi2_device_operation);

#endif

uint8_t fdcan_txdata[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
static void timer1_feedback(void *parameter)
{
    // FDCAN_Transmit(0x01, sizeof(fdcan_txdata), fdcan_txdata);
}

int timer_sample(void)
{
    rt_timer_t timers1 = NULL;

    timers1 = rt_timer_create("timer1", timer1_feedback, RT_NULL, 10, RT_TIMER_FLAG_PERIODIC); // 定时10ms

    if (timers1 != NULL)
    {
        rt_timer_start(timers1);
        rt_kprintf("timer1 create and start! \r\n");
    }
    return 0;
}
MSH_CMD_EXPORT(timer_sample,timer sample used for fdcan test);

int main(void)
{
    /* set LED2 pin mode to output */
    rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
	
    while (1)
    {
        rt_pin_write(LED1_PIN, PIN_HIGH);
        rt_thread_mdelay(200);
        rt_pin_write(LED1_PIN, PIN_LOW);
        rt_thread_mdelay(200);
    }
}


static int ota_app_vtor_reconfig(void)
{
	#define NVIC_VTOR_MASK 0x3FFFFF80
	
	SCB->VTOR = RT_APP_PART_ADDR & NVIC_VTOR_MASK;
	
	return 0;
}
INIT_BOARD_EXPORT(ota_app_vtor_reconfig);

